package dziennik;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import Database.SQLInterface;
import java.sql.SQLException;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Label;


public class Main extends Application {
    Button b_zaloguj;
    TextField tf_login;
    PasswordField pf_haslo;   
    Stage stage;
    GUI gui;
    Label Label_Blad;
    public static BooleanProperty rst = new SimpleBooleanProperty(false);
    
    
    
    void wybierzGUI(){
        
        System.out.println("Typ uzytkownika: " + SQLInterface.userType);
        try {
            switch(SQLInterface.userType){
                case 1:
                    break;

                case 2:
                    break;

                case 3:
                    break;

                case 4:
                        gui = new GUI_Uczen(stage);
                    break;

                default:
                    break;
            }
        }
        catch (Exception e) {
            System.out.println("WybierzGUI(): " + e.getMessage());
            e.printStackTrace(System.out);
        }
    }
    
    
    
    EventHandler<ActionEvent> handler_b_zaloguj = new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent event){
            if(!(tf_login.getText().isEmpty()||pf_haslo.getText().isEmpty())){
                try{
                    SQLInterface.Login(tf_login.getText(), pf_haslo.getText());
                    if(SQLInterface.connected){
                        Label_Blad.setVisible(false);
                        wybierzGUI();
                    }
                    else{
                        Label_Blad.setText("Błędny login lub hasło!");
                        Label_Blad.setVisible(true);
                    }
                }
                catch (SQLException e){
                    Label_Blad.setText("Błąd połączenia z bazą danych!");
                    Label_Blad.setVisible(true);
                }
            }
        }
    };
    
            
    
    @Override
    public void start(Stage stage) throws Exception {
        this.stage=stage;
        final Stage s1 = stage;
  
        
        
        Main.rst.addListener(new ChangeListener<Boolean>(){

            @Override
            public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
                try{
                    Parent ekranLogowania = FXMLLoader.load(getClass().getResource("/gui_fxml/login.fxml"));
                    Scene scene = new Scene(ekranLogowania);
                    s1.setScene(scene); 
                    s1.show();
                    s1.setTitle("Dziennik elektroniczny");

                    b_zaloguj = (Button)ekranLogowania.lookup("#Button_Zaloguj");
                    tf_login = (TextField)ekranLogowania.lookup("#TextField_Login");
                    pf_haslo = (PasswordField)ekranLogowania.lookup("#PasswordField_Haslo");
                    b_zaloguj.setOnAction(handler_b_zaloguj);
                    Label_Blad = (Label)ekranLogowania.lookup("#Label_Blad");
                    Label_Blad.setVisible(false);
                }catch(Exception e){}
            }            
        });
        rst.set(!rst.get());
    }
    
}
